var app = getApp();
Page({
    data:{
        goodsInfo: [],
        page: 1,
        host: app.d.hostUrl,
        minusStatuses: ['disabled', 'disabled', 'normal', 'normal', 'disabled'],
        total: 0,
        selectedAllStatus:'',
    },

    //加载初始化数据
    onLoad:function(options){
        this.loadGoodsData();
        this.sum();
    },

   /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {
        this.loadGoodsData();
    },

    //初始化购物车数据
    loadGoodsData:function(){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/cart-list',
            method: 'get',
            data:{user_id:app.d.userId},
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            success: function(res){
                var status = res.data.status;
                var data = res.data.info;
                if(status == 1){
                    that.setData({
                        goodsInfo:data,
                    });
                }
            },
        });
    },

    //总金额
    sum:function(){
        var that = this;
        var goodsInfo = that.data.goodsInfo;
        var total = 0;
        for(var i=0 ; i<goodsInfo.length ; i++){
            if(goodsInfo[i].selected){
                total += goodsInfo[i].num * goodsInfo[i].price;
            }
        }
        that.setData({
            goodsInfo: goodsInfo,
            total: '¥ ' + total,
        });

    },

    //勾选与取消勾选
    bindCheckbox:function(res){
        var that = this;
        var index = res.currentTarget.dataset.index;
        //当前商品选择的状态
        var selected = that.data.goodsInfo[index].selected;
        var goodsInfo = that.data.goodsInfo;
        goodsInfo[index].selected = !selected;
        that.setData({
            goodsInfo:goodsInfo,
        });
        that.sum();
    },

    //减去购物车货品数量
    bindMinus:function(res){
        var that = this;
        var cartId = res.currentTarget.dataset.cartid;
        var index = res.currentTarget.dataset.index;
        var num = that.data.goodsInfo[index].num;
        if(num == 1){
            wx.showToast({
                title: '操作失败',
                duration: 2000
              });
            return false;
        }
        num--;
        wx.request({
            url: app.d.hostUrl + '/api/cart-reduce',
            method: 'get',
            data: {
                cartId: cartId,
                num: num,
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            success:function(res){
                var status = res.data.status;
                if(status == 0){
                    wx.showToast({
                        title: '操作失败',
                        duration: 2000
                      });
                    return false;
                }else{
                    //购物车数据
                    var goodsInfo = that.data.goodsInfo;
                    //当前商品状态
                    var minusStatuses = that.data.minusStatuses;
                    goodsInfo[index].num = num;
                    minusStatuses[index] = num <= 1? 'disabled' : 'normal';
                    that.setData({
                        goodsInfo: goodsInfo,
                        minusStatuses: minusStatuses,
                    });
                    that.sum();
                }
            },
            error:function(e){
                wx.showToast({
                    title: '网络异常！',
                    duration: 2000
                  });
                return false;
            }
        });
    },

    //增加购物车商品数量
    bindPlus:function(res){
        var that = this;
        var cartId = res.currentTarget.dataset.cartid;
        var index = res.currentTarget.dataset.index;
        var num = that.data.goodsInfo[index].num;
        num++;
        wx.request({
            url: app.d.hostUrl + '/api/cart-reduce',
            method: 'get',
            data: {
                cartId: cartId,
                num: num,
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            success:function(res){
                var status = res.data.status;
                if(status == 0){
                    wx.showToast({
                        title: '操作失败',
                        duration: 2000
                      });
                    return false;
                }else{
                    //购物车数据
                    var goodsInfo = that.data.goodsInfo;
                    //当前商品状态
                    var minusStatuses = that.data.minusStatuses;
                    goodsInfo[index].num = num;
                    minusStatuses[index] = num <= 1? 'disabled' : 'normal';
                    that.setData({
                        goodsInfo: goodsInfo,
                        minusStatuses: minusStatuses,
                    });
                    that.sum();
                }
            },
        });
    },

    //删除购物车某件商品
    removeShopCard:function(res){
        var that = this;
        //要删除的id
        var cartId = res.currentTarget.dataset.cartid;
        //当前购物车商品信息
        var goodsInfo = that.data.goods;
        wx.showModal({
            title: "提示",
            content: "确定删除？",
            success: function(res){
                res.confirm && wx.request({
                    url: app.d.hostUrl + '/api/cart-delete',
                    method: 'get',
                    data:{
                        cartId: cartId,
                    },
                    header: {
                        'Content-Type':  'application/x-www-form-urlencoded'
                    },
                    success:function(res){
                        var status = res.data.status;
                        if(status == 0){
                            wx.showToast({
                                title: '操作失败',
                                duration: 2000
                              });
                            return false;
                        }else{
                            that.loadGoodsData();
                        }
                    },
                    error:function(e){
                        wx.showToast({
                            title: '网络异常！',
                            duration: 2000
                          });
                        return false;
                    }
                });
            },
            fail:function(e){
                wx.showToast({
                    title: '网络异常！',
                    duration: 2000
                  });
                return false;
            }
        });
    },

    //全选
    bindSelectAll:function(res){
        var that = this;
        //当前全选的状态
        var selectedAllStatus = that.data.selectedAllStatus;
        //当前购物车数据
        var goodsInfo = that.data.goodsInfo;
        selectedAllStatus = !selectedAllStatus;
        for(var i=0 ; i<goodsInfo.length; i++){
            goodsInfo[i].selected = selectedAllStatus;
        }

        that.setData({
            goodsInfo:goodsInfo,
            selectedAllStatus:selectedAllStatus,
        });
        that.sum();
    },

    //结算
    bindCheckout:function(res){
        var that = this;
        var goodsInfo = that.data.goodsInfo;
        var toastStr = '';
        for(var i=0;i<goodsInfo.length;i++){
            if(goodsInfo[i].selected){
                toastStr += goodsInfo[i].id;
                toastStr += ',';
            }
        }

        if(toastStr==''){
            wx.showToast({
               title: '请选择要结算的商品！',
               duration: 2000
            });
            return false;
        }
        wx.navigateTo({
          url: '../order/pay?cartId=' + toastStr,
        })
    },

});